﻿<%@ Page Language="C#" MasterPageFile="~/Template.master" AutoEventWireup="true" CodeFile="QuanLySanPham.aspx.cs" Inherits="Employee_QuanLySanPham" Title="Untitled Page" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <link type="text/css" rel="stylesheet" href="../jquery-ui/pager/Pager.css" />
    <script type="text/javascript" src="../jquery-ui/pager/jquery.pager.js"></script>
    <script src="../jquery-ui/jquery.ajax_upload.1.0.min.js" type="text/javascript"></script>
    <link type="text/css" href="../jquery-ui/themes/base/ui.all.css" rel="Stylesheet" />
    <script type="text/javascript" src="../jquery-ui/ui/ui.core.js"></script>
    <script type="text/javascript" src="../jquery-ui/ui/ui.dialog.js"></script>
    <script type="text/javascript" src="../jquery-ui/ui/ui.datepicker.js"></script>
    <style type="text/css">
    .ui-datepicker
    {
        z-index: 1003; /* must be > than popup editor (1002) */
    }
   
</style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script src="../jquery-ui/xml.js" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(document).ready(function(){
            selectProduct(0);
            $("#dialogconfirmdel").dialog({
			    bgiframe: true,
			    autoOpen: false,
			    height: 50,
			    width:450,
			    modal: true
		    });
		    
		    $("#dialogupdate").dialog({
			    bgiframe: true,
			    autoOpen: false,
			    height: 500,
			    width:900,
			    modal: true
		    });
		   
		    $("#txtkeysearch").keyup(function(){
		        current=0;
		        selectProduct(current);
		    });
            
        });
        
        var pageSize=10;
        var current=0;
        
        function selectProduct(page)
        {
            current=page;
            var end=page*pageSize+pageSize;
            var start=pageSize*page+1
            var url="../Controllers/xlSelectSanPham.aspx?time=" + (new Date()).getTime()+"&start="+start+"&end="+end;
            
            if($("#txtkeysearch").attr("value")!="")
            {
                url+="&key="+$("#txtkeysearch").attr("value");
            }
           
            jQuery.get(url,function(xml){
                
                viewResult(xml,page,selectProduct);
            });
        }
        function delSp(frm)
        {
             $("#dialogconfirmdel").html("Bạn có chắc là muốn xoá sản phẩm "+frm.name+"?");
             $("#dialogconfirmdel").dialog('option','buttons',
                {
                    "Không": function(){
                              $("#dialogconfirmdel").dialog('close');
                           },
                    "Xoá":function(){
                        //xoa
                        var id=frm.id.substr(3);
                        var url="../Controllers/xlDeleteSanPham.aspx?time=" + (new Date()).getTime()+"&id="+id;
                        jQuery.get(url,function(res){
                            if(res=="False")
                                alert("Gặp lỗi trong quá trình xoá");
                            selectProduct(current);
                        });
                        
                        $("#dialogconfirmdel").dialog('close');
                        
                    }
                });
               $("#dialogconfirmdel").dialog('open'); 
        }
        function AddLoaiSp()
        {
            
             window.open('../html/AddLoaiSanPham.html','mywin','left=20,top=20,width=600,height=300,toolbar=0,resizable=0');
        }
        function LoadLoaiSp(frm)
        {
             $("#tdloai"+frm.name).html("<img src='../images/waiting.gif'>");
            url="../Controllers/xlSelectLoaiSanPham.aspx?time=" + (new Date()).getTime();
                jQuery.get(url,function(xml1)
                {
                    var xmlDoc1=getXmlDomObject(xml1);
                    var html1="<select id='loai'  >"; 
                    $(xmlDoc1).find('loaisanpham').each(function(){
                        
                        if($(this).find('id').text()==frm.id)
                            html1+="<option selected='yes' value='"+$(this).find('id').text()+"'>"+$(this).find('tenloai').text()+"</option>";
                         else
                            html1+="<option value='"+$(this).find('id').text()+"'>"+$(this).find('tenloai').text()+"</option>";
                    });
                    html1+="</select>";
                    html1+="<input type='button' value='+' onclick='AddLoaiSp()';/><a href='javascript:void(0);' name='"+frm.name+"' id='"+frm.id+"' onclick='LoadLoaiSp(this)'><img src='../images/action_refresh.gif' title='Refresh danh sách'/></a>";
                    $("#tdloai"+frm.name).html(html1);
                });
        }
        function AddNhaSx()
        {
            
             window.open('../html/AddNhaSanXuat.html','mywin','left=20,top=20,width=600,height=300,toolbar=0,resizable=0');
        }
        
        function LoadNhaSX(frm)
        {
            $("#tdnhasx"+frm.name).html("<img src='../images/waiting.gif'>");
            var url="../Controllers/xlSelectNhaSanXuat.aspx?time=" + (new Date()).getTime();
            jQuery.get(url,function(xml1)
                {
                    var xmlDoc1=getXmlDomObject(xml1);
                    var html1="<select id='nhasx' >"; 
                    $(xmlDoc1).find('nhasanxuat').each(function(){
                        
                        if($(this).find('id').text()==frm.id)
                            html1+="<option selected='yes' value='"+$(this).find('id').text()+"'>"+$(this).find('ten').text()+"</option>";
                         else
                            html1+="<option value='"+$(this).find('id').text()+"'>"+$(this).find('ten').text()+"</option>";
                    });
                    html1+="</select>";
                    html1+="<input type='button' value='+'  onclick='AddNhaSx()'/><a href='javascript:void(0);' name='"+frm.name+"' id='"+frm.id+"' onclick='LoadNhaSX(this)'><img src='../images/action_refresh.gif' title='Refresh danh sách'/></a>";
                    $("#tdnhasx"+frm.name).html(html1);
                });
            
        }
        function AddKhuyenMai()
        {
            
             window.open('../html/AddKhuyenMai.html','mywin','left=20,top=20,width=600,height=300,toolbar=0,resizable=0');
        }
        function SelectKm(frm)
        {
            var url="../Controllers/xlSelectKhuyenMaiTheoID.aspx?time=" + (new Date()).getTime()+"&id="+frm.value;
            jQuery.get(url,function(xml){
                var xmlDoc = getXmlDomObject(xml);
                $("#bd").attr("value",$(xmlDoc).find('batdau').text());
                $("#kt").attr("value",$(xmlDoc).find('ketthuc').text());
            });
        }
        function LoadKhuyenMai(frm)
        {
            url="../Controllers/xlSelectKhuyenMai.aspx?time=" + (new Date()).getTime();
                
                jQuery.get(url,function(xml1)
                {
                    
                    var xmlDoc1=getXmlDomObject(xml1);
                    var html1="<select id='khuyenmai' onchange='SelectKm(this)'>";
                    
                    $(xmlDoc1).find('khuyenmai').each(function(){
                        
                        if($(this).find('id').text()==frm.id)
                        {
                            html1+="<option selected='yes' value='"+$(this).find('id').text()+"'>"+$(this).find('hinhthuc').text()+"</option>";
                            $("#bd").attr("value",$(this).find('batdau').text());
                            $("#kt").attr("value",$(this).find('ketthuc').text());
                        }
                        else
                            html1+="<option value='"+$(this).find('id').text()+"'>"+$(this).find('hinhthuc').text()+"</option>";
                    });
                    html1+="</select>";
                    html1+="<input type='button' value='+' onclick='AddKhuyenMai()'/><a href='javascript:void(0);' name='"+frm.name+"' id='"+frm.id+"' onclick='LoadKhuyenMai(this)'><img src='../images/action_refresh.gif' title='Refresh danh sách'/></a>";
                    $("#tdkm"+frm.name).html(html1);
                });
        }
        changeImage=false;
        flag=0;
        
        function openUpdate(frm)
        {
            $("#dialogupdate").dialog('option','title',"Thông tin sản phẩm "+frm.name);
            var id=frm.id.substr(3);
            var url="../Controllers/xlSelectSanPhamTheoID.aspx?time=" + (new Date()).getTime()+"&id="+id;
            jQuery.get(url,function(xml){
                //$("#dialogupdate").dialog('open');
                
                var xmlDoc = getXmlDomObject(xml);
                var html="<table>";
                html+="<tr>";
                html+="<td rowspan=10><img id='linkhinh' src='../"+$(xmlDoc).find('hinh').text()+"' height='300px' width='300px'/></td>";
                html+="<td>Tên Sản Phẩm:</td>";
                html+="<td><input type='text' value='"+$(xmlDoc).find('ten').text()+"' id='ten'  /></td>";
                html+="</tr>";
                html+="<tr>";
                html+="<td>Đơn Giá:</td>";
                html+="<td><input type='text' value='"+$(xmlDoc).find('gia').text()+"' id='gia'  /></td>";
                html+="</tr>";
                html+="<tr>";
                html+="<td>Loại:</td>";
                html+="<td id='tdloai"+$(xmlDoc).find('id').text()+"'></td>";
                url="../Controllers/xlSelectLoaiSanPham.aspx?time=" + (new Date()).getTime();
                jQuery.get(url,function(xml1)
                {
                    var xmlDoc1=getXmlDomObject(xml1);
                    var html1="<select id='loai'  >"; 
                    $(xmlDoc1).find('loaisanpham').each(function(){
                        
                        if($(this).find('id').text()==$(xmlDoc).find('idloai').text())
                            html1+="<option selected='yes' value='"+$(this).find('id').text()+"'>"+$(this).find('tenloai').text()+"</option>";
                         else
                            html1+="<option value='"+$(this).find('id').text()+"'>"+$(this).find('tenloai').text()+"</option>";
                    });
                    html1+="</select>";
                    html1+="<input type='button' value='+' onclick='AddLoaiSp()';/><a href='javascript:void(0);' name='"+$(xmlDoc).find('id').text()+"' id='"+$(xmlDoc).find('idloai').text()+"' onclick='LoadLoaiSp(this)'><img src='../images/action_refresh.gif' title='Refresh danh sách'/></a>";
                    $("#tdloai"+$(xmlDoc).find('id').text()).html(html1);
                });
                html+="</tr>";
                html+="<tr>";
                html+="<td>Nhà Sản Xuất:</td>";
                html+="<td id='tdnhasx"+$(xmlDoc).find('id').text()+"'></td>";
                html+="</tr>";
                url="../Controllers/xlSelectNhaSanXuat.aspx?time=" + (new Date()).getTime();
                
                jQuery.get(url,function(xml1)
                {
                    var xmlDoc1=getXmlDomObject(xml1);
                    var html1="<select id='nhasx' >"; 
                    $(xmlDoc1).find('nhasanxuat').each(function(){
                        
                        if($(this).find('id').text()==$(xmlDoc).find('idnhasx').text())
                            html1+="<option selected='yes' value='"+$(this).find('id').text()+"'>"+$(this).find('ten').text()+"</option>";
                         else
                            html1+="<option value='"+$(this).find('id').text()+"'>"+$(this).find('ten').text()+"</option>";
                    });
                    html1+="</select>";
                    html1+="<input type='button' value='+'  onclick='AddNhaSx()'/><a href='javascript:void(0);' name='"+$(xmlDoc).find('id').text()+"' id='"+$(xmlDoc).find('idnhasx').text()+"' onclick='LoadNhaSX(this)'><img src='../images/action_refresh.gif' title='Refresh danh sách'/></a>";
                    $("#tdnhasx"+$(xmlDoc).find('id').text()).html(html1);
                });
                html+="<tr>";
                html+="<td>Ngày nhập hàng:</td>";
                html+="<td><input type='text' value='"+$(xmlDoc).find('ngaynhap').text()+"' id='ngaynhap' /></td>";
                html+="</tr>";
                html+="<tr>";
                html+="<td>Số lượng còn lại:</td>";
                html+="<td><input type='text' value='"+$(xmlDoc).find('conlai').text()+"' id='soluongcon' /></td>";
                html+="</tr>";
                html+="<tr>";
                html+="<td>Khuyến mãi:</td>";
                html+="<td id='tdkm"+$(xmlDoc).find('id').text()+"'></td>";
                html+="<td>Ngày BD:<input type='text' id='bd' disabled='yes' /></td>";
                html+="<td>Ngày KT:<input type='text' id='kt' disabled='yes' /></td>";
                html+="</tr>";
                url="../Controllers/xlSelectKhuyenMai.aspx?time=" + (new Date()).getTime();
                
                jQuery.get(url,function(xml1)
                {
                    
                    var xmlDoc1=getXmlDomObject(xml1);
                    var html1="<select id='khuyenmai' onchange='SelectKm(this)'>";
                    
                    $(xmlDoc1).find('khuyenmai').each(function(){
                        
                        if($(this).find('id').text()==$(xmlDoc).find('idkhuyenmai').text())
                        {
                            html1+="<option selected='yes' value='"+$(this).find('id').text()+"'>"+$(this).find('hinhthuc').text()+"</option>";
                            $("#bd").attr("value",$(this).find('batdau').text());
                            $("#kt").attr("value",$(this).find('ketthuc').text());
                        }
                        else
                            html1+="<option value='"+$(this).find('id').text()+"'>"+$(this).find('hinhthuc').text()+"</option>";
                    });
                    html1+="</select>";
                    html1+="<input type='button' value='+' onclick='AddKhuyenMai()'/><a href='javascript:void(0);' name='"+$(xmlDoc).find('id').text()+"' id='"+$(xmlDoc).find('idkhuyenmai').text()+"' onclick='LoadKhuyenMai(this)'><img src='../images/action_refresh.gif' title='Refresh danh sách'/></a>";
                    $("#tdkm"+$(xmlDoc).find('id').text()).html(html1);
                });
                html+="<tr>";
                html+="<td >Giảm giá cho VIP :</td>";
                html+="<td id='tdgiam"+$(xmlDoc).find('id').text()+"'>";
                if($(xmlDoc).find('idgiamgia').text()==1)
                    html+="<input type='checkbox' checked /></td>";
                else
                    html+="<input type='checkbox' id='giamgia'/></td>";
                html+="</tr>";
                html+="<tr>";
                html+="<td >Hình mới:</td>";
                old=$(xmlDoc).find('hinh').text();
                html+="<td> <div id='upload_button'><input type='button' value='Upload'></div><input type='button' disabled='disabled' onclick='KhoiPhuc(this)' id='khoiphuc' value='Khôi phụ ảnh cũ'></td>";
                html+="</tr>";
               
                 
               
                html+="</table>";
                
                $("#dialogupdate").html(html);
                newImage="";
                $("#dialogupdate").dialog('option','buttons',
                {
                    "Thoát": function(){
                              $("#dialogupdate").dialog('close');
                              
                              $("#dialogupdate").html("");
                              
                              
                           },
                    "Cập Nhật":function(){
                           
                            if(isNaN($("#gia").attr("value"))==true)
                            {
                                alert("Đơn giá phải là số");
                                return;
                            }
                            if(isNaN($("#soluongcon").attr("value"))==true)
                            {
                                alert("Số lượng phải là số");
                                return;
                            }
                            var ten=$("#ten").attr("value");
                            var gia=$("#gia").attr("value");
                            var loai=$("#loai").attr("value");
                            var nhasx=$("#nhasx").attr("value");
                            var ngaynhap=$("#ngaynhap").attr("value");
                            var soluong=$("#soluongcon").attr("value");
                            var khuyenmai=$("#khuyenmai").attr("value");
                            var giamgia=0;
                            if($("#giamgia").attr("checked")==true)
                                giamgia=1;
                            var url2="../Controllers/xlUpdateSanPham.aspx?time=" + (new Date()).getTime();
                            url2+="&ten="+ten+"&gia="+gia+"&loai="+loai+"&nhasx="+nhasx+"&ngaynhap="+ngaynhap;
                            url2+="&soluong="+soluong+"&khuyenmai="+khuyenmai+"&giamgia="+giamgia+"&id="+$(xmlDoc).find('id').text();
                            if(changeImage==true)
                            {
                             url2+="&hinh="+newImage;
                            }
                            else
                            {
                                url2+="&hinh="+$(xmlDoc).find('hinh').text();
                            }
                            
                            jQuery.get(url2,function(res)
                            {
                              
                                if(changeImage==true)
                                {
                                    var url3="../Controllers/xlDeleteAnh.aspx?time=" + (new Date()).getTime()+"&name="+$(xmlDoc).find('hinh').text();
                        
	                                jQuery.get(url3,function(data){});
                                }
                                $("#dialogupdate").dialog('close');
                                $("#dialogupdate").html("");
                                
                                selectProduct(current);
                            });
                    
                 
                        
                        
                        
                    }
                });
                new Ajax_upload('#upload_button', {
                        action: 'UploadHandler.ashx',
                        onSubmit: function(file, ext) {
                            if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                                // extension is not allowed
                                alert('Error: invalid file extension');
                                // cancel upload
                                return false;
                            }
                            var url3="../Controllers/xlDeleteAnh.aspx?time=" + (new Date()).getTime()+"&name="+newImage;
                            newImage="";
	                        jQuery.get(url3,function(data){});
                            $("#khoiphuc").removeAttr('disabled');
                             $("#khoiphuc").attr('name',file);
                            this.disable();
                            
                        },
                        onComplete: function(file, response) {
                            newImage="ItemImages/"+file;
                            changeImage=true;
                            $("#linkhinh").attr("src","../ItemImages/"+file);
                            this.enable();
                        }
                });
                
                if(flag==0)
                {
                    $("#dialogupdate").bind('dialogclose',function(event,ui){
                        $("#ngaynhap").datepicker('hide',1);
                        if(changeImage==false)
                        {
                            
                            var url3="../Controllers/xlDeleteAnh.aspx?time=" + (new Date()).getTime()+"&name="+newImage;
                            newImage="";
	                        jQuery.get(url3,function(data){});
	                    }
	                    changeImage=false;
                    });
                    flag++;
                }
                $("#ngaynhap").datepicker();
                $("#dialogupdate").dialog('open');
            });
            
        }
        function KhoiPhuc(frm)
	    {
	        changeImage=false;
	        $("#khoiphuc").attr("disabled","disabled");
	        $("#linkhinh").attr("src","../"+old);
	        var url="../Controllers/xlDeleteAnh.aspx?time=" + (new Date()).getTime()+"&name="+frm.name;
	        jQuery.get(url,function(data){});
	        
	    }
        function drawTable(xml)
        {
           var item="<tr>";
           item+="<td>"+$(xml).find('id').text()+"</td>";
           item+="<td>"+$(xml).find('loai').text()+"</td>";
           item+="<td>"+$(xml).find('nhasx').text()+"</td>";
           item+="<td>"+$(xml).find('ten').text()+"</td>";
           item+="<td><img src='../"+$(xml).find('hinh').text()+"' heigth='50px' width='50px' /></td>";
           item+="<td>"+$(xml).find('conlai').text()+"</td>";
           item+="<td>"+$(xml).find('daban').text()+"</td>";
           item+="<td>"+$(xml).find('khuyenmai').text()+"</td>";
           item+="<td>"+$(xml).find('giamgia').text()+"</td>";
           item+="<td>"+$(xml).find('ngaynhap').text()+"</td>";
           item+="<td><input type='button' id='btn"+$(xml).find('id').text()+"' name='"+$(xml).find('ten').text()+"' value='Cập Nhật' onclick='openUpdate(this)'/></td>";
           item+="<td><img src='../images/cancel_48.png' id='del"+$(xml).find('id').text()+"' height='30px' width='30px' name='"+$(xml).find('ten').text()+"' title='xoá "+$(xml).find('ten').text()+"' onclick='delSp(this)'/></td>";
           item+="</tr>";
           return item;
        }
        function viewResult (xml, currentPage,queryFunction)
        {
    		
			var xmlDoc = getXmlDomObject(xml);
			
    		var html = "<table width='100%'>";
    		html+="<tr align='center'><td colspan='12' class='table_header'><strong>Danh Sách Sản Phẩm</strong></td></tr>";
    		var total = $(xmlDoc).find('total').text();
			 
    		var pageCount =  Math.round(total/pageSize+0.499);
    		if (pageCount>1)
    		{
   				
				$("#pager").pager({ pagenumber: currentPage + 1,
    								pagecount: pageCount,
    								buttonClickCallback:
    								function (pageclickednumber)
    								{
    									
										queryFunction(pageclickednumber-1);
    								}
    						  	  });
    		}
    		else
    		{
    			$("#pager").html("");
    		}
    		html+="<tr>";
    		html+="<td>Mã Sản phẩm</td>";
    		html+="<td>Loại Sản Phẩm</td>";
    		html+="<td>Nhà Sản Xuất</td>";
    		html+="<td>Tên Sản Phẩm</td>";
    		html+="<td>Hình ảnh</td>";
    		html+="<td>Hàng Còn Lại</td>";
    		html+="<td>Hàng Đã Bán</td>";
    		html+="<td>Khuyến Mãi</td>";
    		html+="<td>Giảm giá</td>";
    		html+="<td>Ngày Nhập</td>";
    		html+="</tr>";
			$(xmlDoc).find('product').each(function(){
			    var item=drawTable(this);
			    html+=item;
			});
			html+="</table>";
			$("#dssanpham").html(html);
	    } // End of function viewResult
	    
	    function openAdd()
	    {
	       window.location="AddSanPham.aspx";
	        
	    }
    </script>
    
    <div id="searchsanpham">
        Nhập từ khoá tìm kiếm:<input type="text" id="txtkeysearch" />
        <div id='addsanpham'><input type="button" id="them" value="Thêm Sản Phẩm Mới" onclick="openAdd();" /></div>
    </div><br /><br />
    <div id="dssanpham">
    </div>
    <div id="pager"></div>
    <div id="dialogconfirmdel" title="Xác Nhận"></div>
    <div id="dialogupdate" title="Cập nhật thông tin cho sản phẩm"></div>
    <div id="test"></div>
</asp:Content>

